package com.rpc.invoke;

import com.rpc.common.Invocation;
import com.rpc.spring.SpringContextApplication;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

public class RpcInvoke {

    public Object invoke(Class aClass, Invocation invocation) {
        String msg = "";
        try {
            Object bean = SpringContextApplication.getBean(aClass);
            // 获取bean,注意这里用实现类的接口强转去获得目标bean的代理对象，才能成功执行下面的反射方法
            Method method= ReflectionUtils.findMethod(aClass,invocation.getMethodName(),invocation.getParameterTypes());
            // 反射执行方法
            Object invoke= ReflectionUtils.invokeMethod(method,bean,invocation.getParameters());

            return invoke;
        } catch (Exception e) {
            msg = e.toString();
            e.printStackTrace();
        }
        return msg;
    }

}
